package com.changgou.task;

import com.changgou.entity.IdWorker;
import com.changgou.entity.SystemConstants;
import com.changgou.seckill.dao.SeckillGoodsMapper;
import com.changgou.seckill.pojo.SeckillGoods;
import com.changgou.seckill.pojo.SeckillOrder;
import com.changgou.seckill.pojo.SeckillStatus;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

import java.util.Date;

@Component
public class MultiThreadCreateOrder {

    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private IdWorker idWorker;

    @Autowired
    private SeckillGoodsMapper seckillGoodsMapper;

    @Async
    public void createOrder() {
        try {
            System.out.println("开始下单=================:" + Thread.currentThread().getName());
            Thread.sleep(10000);

            SeckillStatus seckillStatus = (SeckillStatus) redisTemplate.boundListOps(SystemConstants.SEC_KILL_USER_QUEUE_KEY).rightPop();
            if (seckillStatus != null) {
                String username = seckillStatus.getUsername();
                Long id = seckillStatus.getGoodsId();
                String time = seckillStatus.getTime();

                // 先从redis中拿到该商品
                SeckillGoods goods = (SeckillGoods) redisTemplate.boundHashOps(SystemConstants.SEC_KILL_GOODS_PREFIX + time).get(id);
                // 有库存的话,新增SeckillOrder
                SeckillOrder seckillOrder = new SeckillOrder();
                seckillOrder.setId(idWorker.nextId());
                seckillOrder.setSeckillId(id);
                seckillOrder.setMoney(goods.getCostPrice());// 秒杀价格
                seckillOrder.setUserId(username);
                seckillOrder.setCreateTime(new Date());
                seckillOrder.setStatus("0");  // 支付状态  0 未支付  1 已支付


                // 将生成的订单存入redis中
                redisTemplate.boundHashOps(SystemConstants.SEC_KILL_ORDER_KEY).put(username, seckillOrder);
                // 判断减少库存的商品库存数量是否还大于0,
                if (goods.getStockCount() <= 0) {
                    // 更新redis中该商品的缓存数据,更新该商品到mysql中
                    seckillGoodsMapper.updateByPrimaryKeySelective(goods);
                    // 如果没有库存了.则直接删除redis中该秒杀商品的缓存
                    redisTemplate.boundHashOps(SystemConstants.SEC_KILL_GOODS_PREFIX + time).delete(id);
                } else {
                    // 有库存则更新redis中该商品的数据信息
                    // redisTemplate.boundHashOps(SystemConstants.SEC_KILL_GOODS_PREFIX + time).put(id, goods);
                }
                // 修改下单的状态
                seckillStatus.setStatus(2);
                seckillStatus.setOrderId(seckillOrder.getId());
                seckillStatus.setMoney(Float.valueOf(seckillOrder.getMoney()));
                redisTemplate.boundHashOps(SystemConstants.SEC_KILL_USER_STATUS_KEY).put(username, seckillStatus);
            }
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("结束下单=================:" + Thread.currentThread().getName());
    }
}
